﻿#pragma execution_character_set("utf-8")
#include "stoast.h"

/* 静态成员变量初始化。 */
QMutex SingleTonSToast::mutex;
QAtomicPointer</*SingleTon*/SToast> SingleTonSToast::instance = 0;

SToast::SToast(QWidget *parent) : QWidget(parent)
{
    msDuration = 5000;                      //初始化成员
	animation = new QPropertyAnimation(this, "windowOpacity");

    setWindowOpacity(0);                    //达到比较好的淡出效果，需要将窗口设置完全透明，否则会有卡顿的效果
    this->setAttribute(Qt::WA_TranslucentBackground, true);         //设置背景透明
    setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
	setStyleSheet("background: black;color: white;border-radius: 5px;");

    setFixedSize(240, 100);                                         //设置为固定窗口大小达到禁止调节窗口大小
    QRect curDesktopRc = QApplication::desktop()->screenGeometry(this);     //获取显示本窗口的屏幕区域
	move((curDesktopRc.width() - width()) / 2,
            (curDesktopRc.height() - height()) / 2);                        //屏幕居中显示
	//Unbtun下有当前桌面扩展， 居中配置：move((QApplication::desktop()->width() - width())/2,(QApplication::desktop()->height() - height())/2);

    labIcon = new QLabel();                                                 //图标、字容器初始化
	label = new QLabel();
    label->setFont(QFont("文泉驿正黑", 16));
	label->setAlignment(Qt::AlignCenter);
    label->adjustSize();                                                    //自适应文本
    label->setWordWrap(true);                                               //自动折行
    label->setMargin(4);                                                    //边距调整

    vLayout = new QVBoxLayout();                                            //初始化布局
	vLayout->addWidget(label);
	vLayout->setAlignment(Qt::AlignCenter);

    hLayout = new QHBoxLayout();                                            //承载图标和vLayout， 设置图标：hLayout->addWidget(labIcon);  预留显示图标
	hLayout->addLayout(vLayout);
	hLayout->setAlignment(Qt::AlignCenter);
	this->setLayout(hLayout);

    connect(&durationTimer, SIGNAL(timeout()), this, SLOT(timeOver()));     //初始化定时器
}

SToast::~SToast()
{
	delete label;
	label = NULL;
	delete labIcon;
	labIcon = NULL;
	delete vLayout;
	vLayout = NULL;
	delete hLayout;
	hLayout = NULL;
}

int SToast::duration() const
{
	return msDuration;
}

void SToast::SetDuration(int msec)
{
	msDuration = msec;
}

void SToast::setMessageVDuration(QString msg, int msecDisplayTime/*=2500*/)
{
	this->show();

	if (!msg.isEmpty())
	{
		label->setText(msg);
	}
	else
	{
		label->setText("");
	}
	label->adjustSize();
	msDuration = msecDisplayTime;

    fadeOutAnimation();                         //淡出画效果
    durationTimer.start(msDuration);            //开始显示计时
}

void SToast::fadeInAnimation()
{
    if (!animation)                             //界面动画，改变透明度的方式消失1 - 0渐变
		return;
    connect(animation, SIGNAL(finished()), this, SLOT(fadeInAnimationFinished()));

	animation->setDuration(1500);
	animation->setStartValue(0.8);
	animation->setEndValue(0);
	animation->start();
}

void SToast::fadeOutAnimation()
{
	if (!animation)                              //界面动画，改变透明度的方式出现0 - 1渐变
		return;
	animation->setDuration(1500);
	animation->setStartValue(0);
	animation->setEndValue(0.8);
	animation->start();
}

void SToast::timeOver()
{
	durationTimer.stop();
    fadeInAnimation();                          //淡入动画效果
}

void SToast::fadeInAnimationFinished()
{
    disconnect(animation, SIGNAL(finished()), this, SLOT(fadeInAnimationFinished()));
	this->close();
}

void SToast::paintEvent(QPaintEvent *event)
{
	QWidget::paintEvent(event);
}
